#!/bin/bash

# 资产管理系统 - 部署包验证脚本
# 版本: 1.0

set -e

# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'

# 日志函数
log_info() {
    echo -e "${BLUE}[INFO]${NC} $1"
}

log_success() {
    echo -e "${GREEN}[SUCCESS]${NC} $1"
}

log_warning() {
    echo -e "${YELLOW}[WARNING]${NC} $1"
}

log_error() {
    echo -e "${RED}[ERROR]${NC} $1"
}

# 验证计数器
TOTAL_CHECKS=0
PASSED_CHECKS=0
FAILED_CHECKS=0

# 检查函数
check_item() {
    local description="$1"
    local command="$2"
    
    TOTAL_CHECKS=$((TOTAL_CHECKS + 1))
    
    if eval "$command" >/dev/null 2>&1; then
        log_success "$description"
        PASSED_CHECKS=$((PASSED_CHECKS + 1))
        return 0
    else
        log_error "$description"
        FAILED_CHECKS=$((FAILED_CHECKS + 1))
        return 1
    fi
}

# 检查文件存在
check_file() {
    local description="$1"
    local file_path="$2"
    
    check_item "$description" "test -f '$file_path'"
}

# 检查目录存在
check_directory() {
    local description="$1"
    local dir_path="$2"
    
    check_item "$description" "test -d '$dir_path'"
}

# 主函数
main() {
    log_info "开始验证部署包完整性..."
    log_info "验证时间: $(date)"
    echo
    
    # 验证核心文件
    log_info "=== 验证核心文件 ==="
    check_file "Dockerfile存在" "Dockerfile"
    check_file "Docker Compose配置存在" "docker-compose.yml"
    check_file "Python依赖文件存在" "requirements.txt"
    check_file "环境配置模板存在" ".env.example"
    echo
    
    # 验证脚本文件
    log_info "=== 验证部署脚本 ==="
    check_file "环境准备脚本存在" "scripts/setup_centos7.sh"
    check_file "一键部署脚本存在" "scripts/deploy.sh"
    check_file "权限设置脚本存在" "scripts/set_permissions.sh"
    check_file "验证脚本存在" "scripts/validate_deployment.sh"
    echo
    
    # 验证维护脚本
    log_info "=== 验证维护脚本 ==="
    check_directory "维护脚本目录存在" "scripts/maintenance"
    check_file "备份脚本存在" "scripts/maintenance/backup.sh"
    check_file "恢复脚本存在" "scripts/maintenance/restore.sh"
    check_file "更新脚本存在" "scripts/maintenance/update.sh"
    check_file "监控脚本存在" "scripts/maintenance/monitor.sh"
    echo
    
    # 验证Docker配置
    log_info "=== 验证Docker配置 ==="
    check_directory "Docker配置目录存在" "docker"
    check_directory "Nginx配置目录存在" "docker/nginx"
    check_file "Nginx主配置存在" "docker/nginx/nginx.conf"
    check_file "Nginx站点配置存在" "docker/nginx/conf.d/asset.conf"
    check_file "Nginx代理参数存在" "docker/nginx/proxy_params"
    echo
    
    check_directory "MySQL配置目录存在" "docker/mysql"
    check_file "MySQL配置文件存在" "docker/mysql/my.cnf"
    check_file "MySQL初始化脚本存在" "docker/mysql/init/01-init.sql"
    echo
    
    check_directory "Redis配置目录存在" "docker/redis"
    check_file "Redis配置文件存在" "docker/redis/redis.conf"
    echo
    
    # 验证监控配置
    log_info "=== 验证监控配置 ==="
    check_directory "Prometheus配置目录存在" "docker/prometheus"
    check_file "Prometheus配置存在" "docker/prometheus/prometheus.yml"
    check_file "Prometheus告警规则存在" "docker/prometheus/alert_rules.yml"
    echo
    
    check_directory "Grafana配置目录存在" "docker/grafana"
    check_directory "Grafana数据源配置目录存在" "docker/grafana/provisioning/datasources"
    check_file "Grafana数据源配置存在" "docker/grafana/provisioning/datasources/datasource.yml"
    check_directory "Grafana仪表板配置目录存在" "docker/grafana/provisioning/dashboards"
    check_file "Grafana仪表板配置存在" "docker/grafana/provisioning/dashboards/dashboard.yml"
    echo
    
    # 验证文档
    log_info "=== 验证文档文件 ==="
    check_directory "文档目录存在" "docs"
    check_file "完整部署指南存在" "docs/DOCKER_DEPLOYMENT_GUIDE.md"
    check_file "快速开始指南存在" "docs/QUICK_START.md"
    check_file "运维手册存在" "docs/MAINTENANCE_MANUAL.md"
    check_file "部署包README存在" "README_DOCKER_DEPLOYMENT.md"
    echo
    
    # 验证脚本语法
    log_info "=== 验证脚本语法 ==="
    if command -v bash >/dev/null 2>&1; then
        check_item "环境准备脚本语法正确" "bash -n scripts/setup_centos7.sh"
        check_item "一键部署脚本语法正确" "bash -n scripts/deploy.sh"
        check_item "备份脚本语法正确" "bash -n scripts/maintenance/backup.sh"
        check_item "恢复脚本语法正确" "bash -n scripts/maintenance/restore.sh"
        check_item "更新脚本语法正确" "bash -n scripts/maintenance/update.sh"
        check_item "监控脚本语法正确" "bash -n scripts/maintenance/monitor.sh"
    else
        log_warning "Bash不可用，跳过脚本语法检查"
    fi
    echo
    
    # 验证Docker配置语法
    log_info "=== 验证Docker配置语法 ==="
    if command -v docker-compose >/dev/null 2>&1; then
        check_item "Docker Compose配置语法正确" "docker-compose config >/dev/null"
    else
        log_warning "Docker Compose不可用，跳过配置语法检查"
    fi
    echo
    
    # 验证文件权限（仅在Linux环境）
    if [[ "$OSTYPE" == "linux-gnu"* ]]; then
        log_info "=== 验证文件权限 ==="
        check_item "环境准备脚本可执行" "test -x scripts/setup_centos7.sh"
        check_item "一键部署脚本可执行" "test -x scripts/deploy.sh"
        check_item "备份脚本可执行" "test -x scripts/maintenance/backup.sh"
        check_item "恢复脚本可执行" "test -x scripts/maintenance/restore.sh"
        check_item "更新脚本可执行" "test -x scripts/maintenance/update.sh"
        check_item "监控脚本可执行" "test -x scripts/maintenance/monitor.sh"
        echo
    fi
    
    # 显示验证结果
    log_info "=== 验证结果汇总 ==="
    echo "总检查项: $TOTAL_CHECKS"
    echo "通过检查: $PASSED_CHECKS"
    echo "失败检查: $FAILED_CHECKS"
    echo
    
    if [[ $FAILED_CHECKS -eq 0 ]]; then
        log_success "🎉 部署包验证通过！所有检查项都正常。"
        echo
        log_info "接下来可以执行部署操作:"
        log_info "1. sudo bash scripts/setup_centos7.sh    # 环境准备"
        log_info "2. bash scripts/deploy.sh                # 一键部署"
        echo
        return 0
    else
        log_error "❌ 部署包验证失败！发现 $FAILED_CHECKS 个问题。"
        echo
        log_info "请检查并修复上述问题后重新验证。"
        echo
        return 1
    fi
}

# 显示使用帮助
show_help() {
    echo "资产管理系统部署包验证脚本"
    echo
    echo "用法: $0 [选项]"
    echo
    echo "选项:"
    echo "  -h, --help    显示此帮助信息"
    echo "  -v, --verbose 显示详细输出"
    echo
    echo "此脚本会验证部署包的完整性，包括:"
    echo "- 核心文件和配置文件"
    echo "- 部署和维护脚本"
    echo "- Docker配置文件"
    echo "- 监控配置文件"
    echo "- 文档文件"
    echo "- 脚本语法和权限"
}

# 解析命令行参数
case "${1:-}" in
    -h|--help)
        show_help
        exit 0
        ;;
    -v|--verbose)
        set -x
        ;;
esac

# 执行主函数
main "$@"